import LogManager from '@/utils/LogManager';
import SettingsManager from '@config/settings';
class Application {
  mediaPlayer: any;

  log(value: any) {
    LogManager.add(`[应用]\t${value}`);
  }

  get douyinVersion() {
    const settings = SettingsManager.getSettings();
    return settings.version === 'normal' ? '抖音' : '抖音极速版';
  }

  openDouyin() {
    launchApp(this.douyinVersion);
  }

  // 播放中奖音乐 媒体
  playPrizeMusic(musicUrl: string) {
    try {
      this.log('开始播放中奖音乐');
      // @ts-ignore
      // 使用媒体播放器播放音乐
      let music = android.media.MediaPlayer();
      music.setDataSource(musicUrl);
      music.prepare();
      music.start();

      // 播放完成后释放资源
      music.setOnCompletionListener(
        // @ts-ignore
        new android.media.MediaPlayer.OnCompletionListener({
          onCompletion: function (mp: any) {
            mp.release();
          },
        }),
      );
    } catch (e: any) {
      this.log('播放音乐失败: ' + e.message);
    }
  }

  // 播放中奖提示音 铃声
  playWinRingSound(musicUrl: string) {
    try {
      if (this.mediaPlayer) {
        this.mediaPlayer.release();
        this.mediaPlayer = null;
      }

      this.log('开始播放中奖音乐');
      // @ts-ignore
      this.mediaPlayer = android.media.MediaPlayer();
      // @ts-ignore
      // 设置音频流类型为铃声
      this.mediaPlayer.setAudioStreamType(android.media.AudioManager.STREAM_RING);
      this.mediaPlayer.setDataSource(musicUrl);
      this.mediaPlayer.prepare();
      this.mediaPlayer.start();

      // @ts-ignore
      // 播放完成后释放资源
      this.mediaPlayer.setOnCompletionListener(
        // @ts-ignore
        new android.media.MediaPlayer.OnCompletionListener({
          onCompletion: function (mp: any) {
            mp.release();
          },
        }),
      );
    } catch (e: any) {
      this.log('播放音乐失败: ' + e.message);
    }
  }

  // 停止音乐播放
  stopWinSound() {
    try {
      if (this.mediaPlayer) {
        if (this.mediaPlayer.isPlaying()) {
          this.mediaPlayer.stop();
        }
        this.mediaPlayer.release();
        this.mediaPlayer = null;
        this.log('已停止播放音乐');
      }
    } catch (e: any) {
      this.log('停止音乐失败: ' + e.message);
    }
  }
}

export default new Application();
